{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "335258be",
   "metadata": {},
   "source": [
    "本章学习关于数据的基础技能，如存储，操作和预处理。我们可以将数据集视作一个表，表的行对应样本，表的列对应属性。\n",
    "矩阵的运算会涉及到线性代数的知识\n",
    "深度学习时关于优化的学习，要找到最好的模型，会涉及到微积分的知识\n",
    "另外机器学习中的预测，会涉及到概率的知识"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "64fed58e",
   "metadata": {},
   "source": [
    "# 2.1 数据操作"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "af267b77",
   "metadata": {},
   "source": [
    "引入张量概念（tensor），与Numpy的ndarray类似，但是深度学习框架比Numpy的ndarray多一些重要功能：1.GPU能很好地支持加速计算，而Numpy仅支持CPU计算；2.张量类支持自动微分，这些功能使得张量更加适合深度学习。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4c813271",
   "metadata": {},
   "source": [
    "### 2.1.1 入门"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "5eced859",
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "af4ce2fc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = torch.arange(12)\n",
    "x"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "96c2f431",
   "metadata": {},
   "source": [
    "可以通过shape属性来访问张量的形状"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "ade9fcf9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([12])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6695e122",
   "metadata": {},
   "source": [
    "也可以通过size或者numel来了解张量里面元素的个数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "9fd84cf9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "12"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.size()\n",
    "x.numel()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8e90d8e9",
   "metadata": {},
   "source": [
    "想要改变一个张量的形状而不改变元素数量和元素值，可以用reshape函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "5181182c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 0,  1,  2,  3],\n",
       "        [ 4,  5,  6,  7],\n",
       "        [ 8,  9, 10, 11]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = x.reshape(3,4)\n",
    "x"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9b0f2c49",
   "metadata": {},
   "source": [
    "我们不用手动计算，只需给出其中一个参数即可，通过-1来调用自动算出维度的功能。即x.reshape(-1,4)和x.reshape(3,-1)效果和x.reshape(3,4)一致"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "131fe7aa",
   "metadata": {},
   "source": [
    "有时我们想设置全0，全1，其他常量或者从特定分布中随机才养的数字来初始化矩阵。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "62f4580a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[0., 0., 0., 0.],\n",
       "         [0., 0., 0., 0.],\n",
       "         [0., 0., 0., 0.]],\n",
       "\n",
       "        [[0., 0., 0., 0.],\n",
       "         [0., 0., 0., 0.],\n",
       "         [0., 0., 0., 0.]]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.zeros((2,3,4)) #全为零的张量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "146cf864",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[1., 1., 1., 1.],\n",
       "         [1., 1., 1., 1.],\n",
       "         [1., 1., 1., 1.]],\n",
       "\n",
       "        [[1., 1., 1., 1.],\n",
       "         [1., 1., 1., 1.],\n",
       "         [1., 1., 1., 1.]]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.ones((2,3,4)) #全为一的张量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "ac55e22e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[-1.4677, -0.1921, -0.6276,  0.3111],\n",
       "        [-0.2338, -0.1760, -0.6125, -0.5130],\n",
       "        [-0.3404, -0.2571, -0.5476,  0.0132]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.randn(3,4) #每个元素从均值为0，标准差为1的标准高斯分布中随机采样"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c0c0e82d",
   "metadata": {},
   "source": [
    "还可以通过列表，来为所需张量中的每个元素赋予确定值。这里最外层的列表对应于轴0，内层的列表对应于轴1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "b2ac0421",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[2, 1, 4, 3],\n",
       "        [1, 2, 3, 4],\n",
       "        [4, 3, 2, 1]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.tensor([[2,1,4,3],[1,2,3,4],[4,3,2,1]])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "74326bd3",
   "metadata": {},
   "source": [
    "### 2.1.2 运算符"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "565895a3",
   "metadata": {},
   "source": [
    "对于任意具有相同形状的张量，常见的标准算术运算符(+,-,*,/和**）都可以升级为按元素运算。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "de20c5d7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([ 3.,  4.,  6., 10.]),\n",
       " tensor([-1.,  0.,  2.,  6.]),\n",
       " tensor([ 2.,  4.,  8., 16.]),\n",
       " tensor([0.5000, 1.0000, 2.0000, 4.0000]),\n",
       " tensor([ 1.,  4., 16., 64.]))"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = torch.tensor([1.0, 2,4,8])\n",
    "y = torch.tensor([2,2,2,2])\n",
    "x+y, x-y, x*y, x/y, x**y"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dde8d141",
   "metadata": {},
   "source": [
    "按元素的方式可以应用更多的计算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "a3ea3743",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([2.7183e+00, 7.3891e+00, 5.4598e+01, 2.9810e+03])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.exp(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5424a079",
   "metadata": {},
   "source": [
    "我们可以把多个张量连结在一起，端对端叠起来形成一个更大的张量。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "b76dfb72",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([[ 0.,  1.,  2.,  3.],\n",
       "         [ 4.,  5.,  6.,  7.],\n",
       "         [ 8.,  9., 10., 11.],\n",
       "         [ 2.,  1.,  4.,  4.],\n",
       "         [ 1.,  2.,  3.,  4.],\n",
       "         [ 4.,  3.,  2.,  1.]]),\n",
       " tensor([[ 0.,  1.,  2.,  3.,  2.,  1.,  4.,  4.],\n",
       "         [ 4.,  5.,  6.,  7.,  1.,  2.,  3.,  4.],\n",
       "         [ 8.,  9., 10., 11.,  4.,  3.,  2.,  1.]]))"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = torch.arange(12, dtype=torch.float32).reshape((3,4))\n",
    "y = torch.tensor([[2.0,1,4,4],[1,2,3,4],[4,3,2,1]])\n",
    "torch.cat((x,y), dim=0), torch.cat((x,y),dim = 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "1ebe1bac",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[False,  True, False, False],\n",
       "        [False, False, False, False],\n",
       "        [False, False, False, False]])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x == y"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "be6d0a90",
   "metadata": {},
   "source": [
    "对张量中所有元素进行求和"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "2d56e106",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor(66.)"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.sum()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "26c64994",
   "metadata": {},
   "source": [
    "### 2.1.3 广播机制"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4c5fdbc2",
   "metadata": {},
   "source": [
    "上面操作是针对相同形状的两个张量，如果形状不相同，仍然可以通过调用广播机制(broadcasting mechanism)来执行元素操作。\n",
    "即通过适当复制元素来扩展一个或两个数组，以便在转换之后，使得形状相同，能够按照元素操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "015a07bd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([[0],\n",
       "         [1],\n",
       "         [2]]),\n",
       " tensor([[0, 1]]))"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = torch.arange(3).reshape((3,1))\n",
    "b = torch.arange(2).reshape((1,2))\n",
    "a,b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "a80d493f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0, 1],\n",
       "        [1, 2],\n",
       "        [2, 3]])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a + b #这里将a，b进行了扩展形成3x2的矩阵后，按元素进行计算"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b1081015",
   "metadata": {},
   "source": [
    "### 2.1.4 索引和切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "990cbdc5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([ 8.,  9., 10., 11.]),\n",
       " tensor([[ 4.,  5.,  6.,  7.],\n",
       "         [ 8.,  9., 10., 11.]]))"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x[-1],x[1:3] #与python中列表的操作一致"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dd561369",
   "metadata": {},
   "source": [
    "除了读取外，还可以通过指定索引将元素写入矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "f1015688",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 0.,  1.,  2.,  3.],\n",
       "        [ 4.,  5.,  9.,  7.],\n",
       "        [ 8.,  9., 10., 11.]])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x[1,2]=9\n",
    "x"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dcb6b1a9",
   "metadata": {},
   "source": [
    "如果我们想为多个元素赋值相同的值，我们只需索引所有元素即可"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "10f81731",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[12., 12., 12., 12.],\n",
       "        [12., 12., 12., 12.],\n",
       "        [ 8.,  9., 10., 11.]])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x[0:2,:] = 12 #第一行到第二行，所有的列赋值12\n",
    "x"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a1f19857",
   "metadata": {},
   "source": [
    "### 2.1.5 节省内存"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c1d30b77",
   "metadata": {},
   "source": [
    "运行一些操作可能会导致为新结果分配内存。 Y = X + Y，将取消引用Y指向的张量，而是指向新分配的内存处的张量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "6c290847",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "before = id(y)\n",
    "y = y + x\n",
    "id(y) == before"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "e39c55d4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "id(z): 2035122032208\n",
      "id(z): 2035122032208\n"
     ]
    }
   ],
   "source": [
    "z = torch.zeros_like(y)\n",
    "print('id(z):', id(z))\n",
    "z[:] = x + y\n",
    "print('id(z):', id(z))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "92a2484c",
   "metadata": {},
   "source": [
    "我们也可以通过x[:]= x + y或x+=y来减少内存开销"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "71e0f3ea",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "before = id(y)\n",
    "y += x\n",
    "id(y) == before"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d0b4ee22",
   "metadata": {},
   "source": [
    "### 2.1.6 转换为其他Python对象"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eddda424",
   "metadata": {},
   "source": [
    "将张量转换为Numpy（ndarray）很容易，同事torch张量和numpy数组将共享底层内存，因此就地操作一个张量也会同时更改另一个张量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "1ac338a8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(numpy.ndarray, torch.Tensor)"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = x.numpy()\n",
    "B = torch.tensor(A)\n",
    "type(A), type(B)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6e5d4c5b",
   "metadata": {},
   "source": [
    "将大小为1的张量转换为python标量，可以用item函数或python内置函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "99afcedc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([3.5000]), 3.5, 3.5, 3)"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = torch.tensor([3.5])\n",
    "a, a.item(), float(a), int(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5d3f4ca0",
   "metadata": {},
   "source": [
    "### 2.1.7 小结"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3d5a5c19",
   "metadata": {},
   "source": [
    "本结引入了张量的概念，以及基本数学运算，广播，索引，切片，内存节省和转换其他python对象"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "838511e3",
   "metadata": {},
   "source": [
    "### 2.1.8 练习"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "db1bdef8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[False, False, False, False],\n",
       "        [False, False, False, False],\n",
       "        [False, False, False, False]])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x > y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "78faae39",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[True, True, True, True],\n",
       "        [True, True, True, True],\n",
       "        [True, True, True, True]])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x < y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "2cd45691",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([[[ 0,  1,  2,  3,  4,  5],\n",
       "          [ 6,  7,  8,  9, 10, 11]]]),\n",
       " tensor([[[ 0,  1,  2,  3,  4,  5]],\n",
       " \n",
       "         [[ 6,  7,  8,  9, 10, 11]],\n",
       " \n",
       "         [[12, 13, 14, 15, 16, 17]]]),\n",
       " tensor([[[ 0,  2,  4,  6,  8, 10],\n",
       "          [ 6,  8, 10, 12, 14, 16]],\n",
       " \n",
       "         [[ 6,  8, 10, 12, 14, 16],\n",
       "          [12, 14, 16, 18, 20, 22]],\n",
       " \n",
       "         [[12, 14, 16, 18, 20, 22],\n",
       "          [18, 20, 22, 24, 26, 28]]]))"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = torch.arange(12).reshape((1,2,6))\n",
    "b = torch.arange(18).reshape((3,1,6))\n",
    "a, b, a+b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c6dc9f04",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
